<?php
declare(strict_types=1);

namespace lib\data\traits;

use sys\Log;

trait MidTrait {
    static array $mid_cache = [];
    static array $pk = [];

    protected function traitInit(){
        static::$pk[$this->id] = $this;
        static::$mid_cache[$this->mid][$this->sortid] = $this;
    }

    public static function getAll(int $mid){
        Log::write('getAll 3 = '. json_encode(static::$mid_cache, JSON_UNESCAPED_UNICODE), 'DEBUG');
        return array_values(static::$mid_cache[$mid]??[]);
    }

    public static function get(int $id) :?static{
        return static::$pk[$id]??null;
    }

    public function remove(){
        unset(static::$pk[$this->id]);
        unset(static::$mid_cache[$this->mid][$this->sortid]);
    }
}